import cv2
import time

def save_frames_at_intervals(video_path, interval_seconds, output_folder):
    # 打开视频文件
    cap = cv2.VideoCapture(video_path)
    
    if not cap.isOpened():
        print("无法打开视频文件")
        return
    
    # 获取视频基本信息
    fps = cap.get(cv2.CAP_PROP_FPS)
    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    
    print(f"视频信息：{fps} FPS, 总帧数：{total_frames}, 分辨率：{frame_width}x{frame_height}")

    # 计算间隔帧数（秒 × 帧率）
    interval_frames = int(interval_seconds * fps)
    current_frame = 0
    saved_count = 0

    while cap.isOpened():
        ret, frame = cap.read()
        
        if not ret:
            break  # 视频结束

        # 检查是否到达保存间隔
        if current_frame % interval_frames == 0:
            # 生成带时间戳的文件名
            timestamp = int(time.time() * 1000)
            output_path = f"{output_folder}/frame_{timestamp}_{current_frame}.jpg"
            cv2.imwrite(output_path, frame)
            print(f"已保存：{output_path}")
            saved_count += 1

        current_frame += 1

    # 释放资源
    cap.release()
    print(f"处理完成，共保存 {saved_count} 帧图像")

# 使用示例
video_path = "/media/starkpid/Document/flyList/project/03yumaoqiu/yumaoqiu.mp4"       # 视频文件路径
output_folder = "/home/starkpid/pro/data/data_raw"      # 输出文件夹路径
interval_seconds = 0.2                # 保存间隔（秒）

save_frames_at_intervals(video_path, interval_seconds, output_folder)